<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/defaultconf.mex");
$__areatitle[1] = 'Remessas a Tratamento';
$__areatitle[2] = 'Remessas a Pagamento';
$_coltype[0] = '';
$_coltype[1] = 'URG';
$_colmodo[1] = 'Enviar';
$_colmodo[2] = 'Pagar';

$TotalPay = 0;

$CorrCond = '';
if (isset($_POST['apay_Correspondent']) && $_POST['apay_Correspondent'] > 0) {
    $CorrCond = ' AND FraIncCorresponsal = ' . $_POST['apay_Correspondent'];
}
$BankCond = '';
if (isset($_POST['apay_Bank']) && $_POST['apay_Bank'] > 0) {
    $BankCond = ' AND FraBancoID = ' . $_POST['apay_Bank'];
}
MYTCONN();
$GET = $GLOBALS['MYT']->db_array("SELECT FraID,FraFechaIngreso,FraAlbaran,
  FraUrgente,FraARecibir,FraEnviado,FraCargo,FraTasa,FraBnfNombre,sg_Abreviatura AS FraStatus, FraModo,
  FraBnfDocType,FraBnfCedula,FraBancoID,FraCiudadSucursal,FraSucursal,FraTipoCuenta,FraCuenta
   FROM facturas
   INNER JOIN facturasextjoins ON FraID = FraExtID
   INNER JOIN facturas_income ON FraIncID = FraID
   INNER JOIN hd_statusfacturas ON sg_ID = FraStatus
   LEFT OUTER JOIN hd_bancos ON bp_Country = FraClPais AND bp_Name = FraBancoID
   WHERE FraModo = 2 AND FraModoPago = 1 AND FraAlbaran > 0 AND FraNumero = 0 AND FraStatus = 16" . $CorrCond . $BankCond);
$CGET = $GET[0];
$rsStr = '';
for ($g = 1; $g <= $CGET; $g++) {
    $TotalPay += $GET[$g]['FraARecibir'];
    $rsStr .= '[' . $GET[$g]['FraID'] . ',\'' . $_colmodo[$GET[$g]['FraModo']] . '\',\'' . $GET[$g]['FraBnfNombre'] . '\',
		\'' . $GET[$g]['FraBancoID'] . '\',\'' . $GET[$g]['FraCuenta'] . '\',' . number_format($GET[$g]['FraARecibir'], 2, '.', '') . ',\'' . $GET[$g]['FraFechaIngreso'] . '\',\'' . $GET[$g]['FraStatus'] . '\'],';
}
?>

<div id="rsDisplay"></div>
<script charset="utf-8" type="text/javascript">
    disableSelection(document.getElementById("rsDisplay"));

    Ext.onReady(function(){

        $elem = 205;
        var  windowWidth = 0, windowHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            windowWidth = window.innerWidth
            windowHeight = window.innerHeight-$elem;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight-$elem;
        }

        Ext.QuickTips.init();

        var myData = [<?php echo substr($rsStr, 0, strlen($rsStr) - 1); ?>];

        var ds = new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, [
                {name: 'FraID', type: 'int'},
                {name: 'FraModo', type: 'string'},
                {name: 'FraBnfNombre', type: 'string'},
                {name: 'FraBancoID', type: 'string'},
                {name: 'FraCuenta', type: 'string'},
                {name: 'FraARecibir', type: 'float'},
                {name: 'Data', type: 'date', dateFormat: 'Y-m-d H:i:s'},
                {name: 'FraStatus', type: 'string'}
            ])
        });
        ds.loadData(myData);
        var colModel = new Ext.grid.ColumnModel([
            {header: "Operacao", width: 60, sortable: true, locked:true, dataIndex: 'FraID'},
            {header: "Canal", width: 60, sortable: true, dataIndex: 'FraModo'},
            {header: "Destinatario", width: 300, sortable: true, locked:true, dataIndex: 'FraBnfNombre'},
            {header: "Banco Pagamento", width: 200, sortable: true, locked:true, dataIndex: 'FraBancoID'},
            {header: "Numero de Conta", width: 200, sortable: true, locked:true, align: 'right',dataIndex: 'FraCuenta'},
            {header: "A Pagar", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'FraARecibir'},
            {header: "Data", width: 120, sortable: true, locked:true, renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),dataIndex: 'Data'},
            {header: "Estado", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'FraStatus'}
        ]);
        Bars = new Ext.Toolbar({
            height:28,
            store: this.store,
            items : ["A pagamento: <?php echo $CGET; ?> Pagamentos, EUR: <?php echo number_format($TotalPay, 2); ?>"," | Seleccionado: 0 | ",{
                    type:'button',
                    loadMask: true,
                    text:'Confirmar Pagamentos',
                    handler:function(){
                        var ss = '';
                        var m = selmode.getSelections();
                        for(var i = 0, len = m.length; i < len; i++){
                            ss += m[i].get("FraID") + ",";}
                        document.apayform.SelectedPayments.value = ss;
                        document.apayform.action='_response/rsproxy.php?pr=fn_invoiceaccountpayment';
                        subform('apayform','rsPaymentResponse');
                    }
                },{
                    type:'button',
                    loadMask: true,
                    text:'Imprimir Folha de Pagamentos',
                    handler:function(){
                        var ss = '';
                        var m = selmode.getSelections();
                        for(var i = 0, len = m.length; i < len; i++){
                            ss += m[i].get("FraID") + ",";}
                        document.apayform.SelectedPayments.value = ss;
			document.apayform.action='_response/rsproxy.php?pr=fn_remittancepaymentprint';
                        subform('apayform','rsPaymentResponse');
                    }
                },"<div id=rsPaymentResponse style=float:right></div>"]
        });

        var selmode = new Ext.grid.RowSelectionModel({});
        var Console = new Ext.grid.GridPanel({
            height: windowHeight,
            width: windowWidth-37,
            ds: ds,
            cm: colModel,
            sm: selmode,
            listeners: {
                rowdblclick: function(ds, row) {
                    var record = ds.getStore().getAt(row);
                    var value = record.get('FraID');
                    menuwindow('Operativo','_response/call.php?get=Operative/pp_frm&OR='+value,800,520,'MnMain');
                }
            },
            bbar: Bars,
            renderTo: 'rsDisplay'
        });
    });
</script>